package com.ushio.authservice.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ushio.authservice.openfeign.DogKingDomFeign;
import com.ushio.authservice.user.entity.qo.LoginUserQo;
import com.ushio.authservice.user.entity.vo.LoginUserVo;
import com.ushio.authservice.user.entity.qo.UserAddQo;
import com.ushio.authservice.user.mapper.UserMapper;
import com.ushio.authservice.user.service.UserService;
import com.ushio.commonmodule.openfeign.entity.DogCharacterOpnFeign;
import com.ushio.commonmodule.pojo.vo.UserSeasonEventVo;
import com.ushio.commonmodule.utils.JwtUtil;
import com.ushio.commonmodule.utils.RedisDbSwitcher;
import com.ushio.commonmodule.pojo.user.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author 19451
 */

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    RedisDbSwitcher redisDbSwitcher;

    @Autowired
    DogKingDomFeign dogKingDomFeign;

    @Override
    public List<User> getUserAll(){

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        List<User> users = userMapper.selectList(lambdaQueryWrapper);

        return users;
    }

    @Override
    @Transactional
    public User addUser(UserAddQo userAddQo) {

        User users = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUserName, userAddQo.getUserName()));

        if (users!=null){
            throw new RuntimeException("该用户名已经注册");
        }

        String encode = new BCryptPasswordEncoder().encode(userAddQo.getPassWord());

        userAddQo.setPassWord(encode);

        User user = User.builder().build();

        user.setCreateTime(new Date());

        BeanUtils.copyProperties(userAddQo,user);

        userMapper.insert(user);

        UserSeasonEventVo userSeasonEventVo = dogKingDomFeign.initializeSeasonEvent(user);

        DogCharacterOpnFeign dogCharacterOpnFeign = dogKingDomFeign.initializeDogCharacter(user);

        return user;
    }

    @Override
    public LoginUserVo getLogin(LoginUserQo loginUserQo) {

        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();

        userLambdaQueryWrapper.eq(User::getUserName,loginUserQo.getUserName());

        User user = userMapper.selectOne(userLambdaQueryWrapper);
        if (user==null){
            throw new RuntimeException("用户名或者密码错误");
        }
        String passWord = user.getPassWord();

        boolean checkpw = BCrypt.checkpw(loginUserQo.getPassWord(), passWord);

        if (!checkpw){
            throw new RuntimeException("用户名或者密码错误");
        }

        redisDbSwitcher.writeToDb(5,user.getUserId().toString(),user.toString());

        stringRedisTemplate.opsForValue().set(user.getUserId().toString(),user.toString());

//        String token = "";
        String token = JwtUtil.generateToken(user);

        LoginUserVo loginUserVo = LoginUserVo.builder().loginMsg("登录成功").token(token).build();

        BeanUtils.copyProperties(user,loginUserVo);

        return loginUserVo;
    }



}
